/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.loja.controller;

import br.com.loja.model.entity.cliente.Cliente;
import br.com.loja.model.entity.compra.Compra;
import br.com.loja.model.entity.conta.Conta;
import br.com.loja.model.entity.despesa.CartaoCreditoCompra;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import br.com.loja.model.entity.despesa.Despesa;
import br.com.loja.model.entity.estoque.AlteracaoItemEstoque;
import br.com.loja.model.entity.estoque.ItemEstoque;
import br.com.loja.model.entity.produto.Categoria;
import br.com.loja.model.entity.produto.Grupo;
import br.com.loja.model.entity.produto.Produto;
import br.com.loja.model.entity.transacao.impl.PagamentoVendaCartao;
import br.com.loja.model.entity.venda.Venda;
import br.com.loja.model.service.exception.ServiceException;
import br.com.loja.view.swing.DefaultEntityEditor;
import br.com.loja.view.swing.MainDesktopFrame;

/**
 *
 * @author CASA
 */
public class MainDesktopFramePresenter implements ActionListener {

    private final MainDesktopFrame mainDesktopFrame;

    public MainDesktopFramePresenter(MainDesktopFrame mainDesktopFrame) {
        this.mainDesktopFrame = mainDesktopFrame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        try {

            switch (e.getActionCommand()) {

                case "grupos":
                    addInternalFrame(new DefaultEntityEditor("groups", Grupo.class));
                    break;

                case "categorias":
                    addInternalFrame(new DefaultEntityEditor("categories", Categoria.class));
                    break;

                case "produtos":
                    addInternalFrame(new DefaultEntityEditor("products", Produto.class));
                    break;

                case "itensEstoque":
                    addInternalFrame(new DefaultEntityEditor("no title", ItemEstoque.class));
                    break;

                case "alteracaoEstoque":
                    addInternalFrame(new DefaultEntityEditor("no title", AlteracaoItemEstoque.class));
                    break;

                case "incluirCompra":
                    addInternalFrame(new DefaultEntityEditor("no title", Compra.class));
                    break;

                case "cartoesCreditoCompra":
                    addInternalFrame(new DefaultEntityEditor("no title", CartaoCreditoCompra.class));
                    break;

                case "despesas":
                    addInternalFrame(new DefaultEntityEditor("expenses", Despesa.class));
                    break;

                case "clientes":
                    addInternalFrame(new DefaultEntityEditor("no title", Cliente.class));
                    break;

                case "novaVenda":
                    addInternalFrame(new DefaultEntityEditor("no title", Venda.class));
                    break;

                case "contas":
                    addInternalFrame(new DefaultEntityEditor("no title", Conta.class));
                    break;

                case "testarConexao":
                    addInternalFrame(new DefaultEntityEditor("no title", PagamentoVendaCartao.class));
                    break;

                case "sair":
                    System.exit(0);
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "Em Construção", "Loja App", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (ServiceException ex) {
            System.err.println(ex.getMessage());
            JOptionPane.showMessageDialog(null, "Erro ao iniciar serviço", "Loja App", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
    }

    private void addInternalFrame(JInternalFrame frame) throws RuntimeException, ServiceException {

        final JDesktopPane desktopPane = mainDesktopFrame.getDesktopPane();

        if (!desktopPane.equals(frame.getDesktopPane())) {
            desktopPane.add(frame);
            frame.setVisible(true);
        } else {
            try {

                desktopPane.setSelectedFrame(frame);

                if (frame.isIcon()) {
                    frame.setIcon(false);
                }

                frame.setMaximum(true);
            } catch (PropertyVetoException ex) {
                throw new RuntimeException("Houve em erro ao iniciar o objeto", ex);
            }
        }
    }
}
